Widget with class

  • Code

    We can create widgets in two primary ways: with a class or without a class.

    1. Widget Without a Class (Function or Stateless):

    
                      import 'package:flutter/material.dart';
    
                      Widget myFunctionWidget() {
                        return Container(
                          child: Text('Hello, Function Widget!'),
                        );
                      }
    
    
                      

    2. Widget with a Class

    StatelessWidget
    
                       import 'package:flutter/material.dart';
    
    class MyStatelessWidget extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Container(
          child: Text('Hello, Stateless Widget!'),
        );
      }
    }
    
    
    
    StatefulWidget
    
    import 'package:flutter/material.dart';
    
    class MyStatefulWidget extends StatefulWidget {
      @override
      _MyStatefulWidgetState createState() => _MyStatefulWidgetState();
    }
    
    class _MyStatefulWidgetState extends State<MyStatefulWidget> {
      @override
      Widget build(BuildContext context) {
        return Container(
          child: Text('Hello, Stateful Widget!'),
        );
      }
    }